<template>
  <div class="form"
    style="height:100%">
    <el-form :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="140px"
      size="small"
      style=" display: flex;flex-direction: column;align-items: center;justify-content: center;height:100%" class="demo-ruleForm">
      <el-col :span="8"
        style="line-height: 100px;
         font-size: 20px;
         font-weight: 600;">
        注&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;册
      </el-col>
      <el-col :span="8">
        <el-form-item label="用户名"
          prop="name">
          <el-input type="name"
            v-model="ruleForm.name"
            autocomplete="off"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="密码"
          prop="pass">
          <el-input type="password"
            v-model="ruleForm.pass"
            autocomplete="off"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="确认密码"
          prop="checkPass">
          <el-input type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="手机号"
          prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="邮箱"
          prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
      </el-col>
      <el-form-item>
        <el-button @click="$router.push('/login')">已注册，去登录</el-button>
        <el-button type="primary"
          @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "index",
  data () {
    var validatePass = (rule, value, callback) => {
      let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$/
      if (!reg.test(value)) {
        callback(new Error('密码必须是由4-20位字母+数字组合'))
      } else {
        callback()
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    }
    return {
      ruleForm: {
        name: '',
        pass: '',
        checkPass: '',
        phone: '',
        email: '',
      },
      rules: {
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        pass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入11位手机号码', trigger: 'blur' },
          {
            pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
            message: '请输入正确的手机号码'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      }
    };
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>